package me.leiusual.sharing.ui.mine

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.fragment.findNavController
import dagger.android.support.DaggerFragment
import kotlinx.android.synthetic.main.fragment_mine_mobile.*
import me.leiusual.sharing.R
import me.leiusual.sharing.repository.model.Data
import me.leiusual.sharing.ui.helper.HandleObserver
import me.leiusual.sharing.util.extension.toImplicitMobile
import me.leiusual.sharing.util.extension.toast
import timber.log.Timber
import javax.inject.Inject

/**
 * Create by leiusual on 2018/7/25
 */
class MineMobileFragment : DaggerFragment() {

  @Inject
  lateinit var factory: ViewModelProvider.Factory

  private val viewModel: MineMobileViewModel by viewModels { factory }

  override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    return inflater.inflate(R.layout.fragment_mine_mobile, container, false)
  }

  override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    toolbar.setNavigationOnClickListener {
      findNavController().popBackStack()
    }

    text_view_send_sms_old.setOnClickListener {
      viewModel.sendSMSOld(viewModel.user.mobile)
    }

    text_view_send_sms.setOnClickListener {
      val mobile = edit_text_mobile.text.toString()
      viewModel.sendSMS(mobile)
    }

    button_confirm.setOnClickListener {
      val verifyCode = edit_text_code_old.text.toString()
      val mobile = edit_text_mobile.text.toString()
      val checkCode = edit_text_code.text.toString()
      viewModel.mobileUpdate(verifyCode, mobile, checkCode)
    }

    updateUserInfo()

    observeData()
  }

  private fun observeData() {
    viewModel.sendSMSOld.observe(viewLifecycleOwner, HandleObserver {
      Timber.v("sendSMS = %s", it)
      when (it) {
        is Data.Loading -> {
          text_view_send_sms_old.isEnabled = false
        }
        is Data.Error -> {
          text_view_send_sms_old.isEnabled = true
          toast(it.error.message)
        }
        is Data.Success -> {
          text_view_send_sms_old.startTimber()
          toast("短信发送成功")
        }
      }
    })

    viewModel.sendSMS.observe(viewLifecycleOwner, HandleObserver {
      Timber.v("sendSMS = %s", it)
      when (it) {
        is Data.Loading -> {
          text_view_send_sms.isEnabled = false
        }
        is Data.Error -> {
          text_view_send_sms.isEnabled = true
          toast(it.error.message)
        }
        is Data.Success -> {
          text_view_send_sms.startTimber()
          toast("短信发送成功")
        }
      }
    })

    viewModel.mobileUpdate.observe(viewLifecycleOwner, HandleObserver {
      Timber.v("mobileUpdate = %s", it)
      when (it) {
        is Data.Loading -> {
          button_confirm.isEnabled = false
        }
        is Data.Error -> {
          button_confirm.isEnabled = true
          toast(it.error.message)
        }
        is Data.Success -> {
          toast("修改成功")
          // 成功了就先赋值当前输入的手机号到xml中
          viewModel.user.mobile = edit_text_mobile.text.toString()
          findNavController().popBackStack()
        }
      }
    })
  }

  private fun updateUserInfo() {
    if (viewModel.user.mobile.isNullOrBlank()) {
      layout_mobile_old_info.visibility = View.GONE
    } else {
      layout_mobile_old_info.visibility = View.VISIBLE
      text_view_mobile.text = "验证码接收手机号码为：${viewModel.user.mobile.toImplicitMobile()}"
    }
  }
}